package com.eerl.pdev;

import com.eerl.pdev.data.ErlDevDataConfigOpenAction;
import com.eerl.pdev.data.ErlLocalDataConfigOpenAction;
import com.eerl.pdev.debug.EVarWatchAddToWatchAction;
import com.eerl.pdev.debug.EVarWatchCloseAction;
import com.eerl.pdev.debug.EVarWatchStartAction;
import com.eerl.pdev.debug.TestAction;
import com.eerl.pdev.terminal.cmd.TerminalActionMMake;
import com.eerl.pdev.terminal.cmd.TerminalActionMakeThis;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.keymap.Keymap;
import com.intellij.openapi.keymap.KeymapManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class ErlActionGroup extends DefaultActionGroup {

    public ErlActionGroup() {
        super();
        registerShortcutForEVarWatchAddToWatchAction();
    }

    // xll_todo 这块最好优化下,要不统一放代码, 要不就统一放xml
    @Override
    public AnAction @NotNull [] getChildren(@Nullable AnActionEvent e) {
        if (MySettingsState.getInstance().Is_DEBUG) {
            return new AnAction[] {
                    new TerminalActionMakeThis(),
                    new TerminalActionMMake(),
                    Separator.getInstance(),  // 分割线
                    new ErlLocalDataConfigOpenAction(),
                    new ErlDevDataConfigOpenAction(),
                    // Separator.getInstance(),
                    // new TerminalEnvPrint(),
                    Separator.getInstance(),
                    new EVarWatchAddToWatchAction(),
                    new EVarWatchStartAction(),
                    new EVarWatchCloseAction(),
                    Separator.getInstance(),
                    new TestAction()
            };
        } else {
            return new AnAction[] {
                    new TerminalActionMakeThis(),
                    new TerminalActionMMake(),
                    Separator.getInstance(),  // 分割线
                    new ErlLocalDataConfigOpenAction(),
                    new ErlDevDataConfigOpenAction(),
                    Separator.getInstance(),
                    new EVarWatchAddToWatchAction(),
                    new EVarWatchStartAction(),
                    new EVarWatchCloseAction(),
            };
        }

    }

    private void registerShortcutForEVarWatchAddToWatchAction() {
        ActionManager actionManager = ActionManager.getInstance();
        String actionId = "EVarWatchAddToWatchAction";

        // 检查 Action 是否已经注册
        AnAction action = actionManager.getAction(actionId);
        if (action == null) {
            // 如果 Action 未注册，则注册它
            action = new EVarWatchAddToWatchAction();
            actionManager.registerAction(actionId, action);
        }

        // 为 Action 添加快捷键
        KeymapManager keymapManager = KeymapManager.getInstance();
        Keymap activeKeymap = keymapManager.getActiveKeymap();

        // 创建快捷键 (例如: Ctrl+Alt+W)
        KeyboardShortcut shortcut = new KeyboardShortcut(KeyStroke.getKeyStroke("control alt W"), null);

        // 移除现有的快捷键（如果有）
        for (Shortcut existingShortcut : activeKeymap.getShortcuts(actionId)) {
            activeKeymap.removeShortcut(actionId, existingShortcut);
        }

        // 添加新的快捷键
        activeKeymap.addShortcut(actionId, shortcut);
    }
}
